<?php

require_once 'vtigerwebservices.php';
require_once 'Utils.php';

$wsObj = new VTWebservices('http://localhost/crm54/webservice.php', 'admin', 'fPdqe7FluhZAA5vA');
$wsObj->login();
//$desc = $wsObj->query('select * from Calendar;');
//echo "<pre>";print_r($desc);die;
$fileName = 'task.csv';

$fileHandler = fopen($fileName, 'r');
$currentRow = 0;
$headers = array();
$rows = array();

while($data = fgetcsv($fileHandler, 0, ',')) {
	if($currentRow == 0) {
		$headers = CSV_Import_Utils::createFieldMap($data);
	} else {
		$mappedData = array();
		$allValuesEmpty = true;
		foreach($headers as $fieldName => $index) {
			$fieldValue = $data[$index];
			if($fieldName == 'date_start') {
				list($dateStart, $timeStart) = explode(' ', $fieldValue);
				list($mm, $dd, $yy) = explode('/', $dateStart);
				$dateStart = "$yy-$mm-$dd";
				$mappedData['date_start'] = $dateStart;
				$mappedData['time_start'] = $timeStart;
				$mappedData['due_date'] = $dateStart;
				$mappedData['time_end'] = $timeStart;
			} else {
				$mappedData[$fieldName] = $fieldValue;
			}
		}
		$mappedData['activitytype'] = 'Task';
		$mappedData['sendnotification'] = 0;
		$mappedData['assigned_user_id'] = '19x1';
		try {
			$desc = $wsObj->create($mappedData, 'Calendar');
		} catch (Exception $e) {
			echo "failed to create";die;
		}
	}
	$currentRow++;
}
$wsObj->logout();


